package com.manning.readinglist.config;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @Classname DatasourceConfig
 * @Description TODO
 * @Date 2020/5/21 11:32
 * @Created by seed
 */
@Configuration
public class DatasourceConfig {

    @Value("${spring.datasource.url}")
    private String url;

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DatasourceProperties datasourceProperties() {
        return new DatasourceProperties();
    }

    @Autowired
    private DatasourceProperties datasourceProperties;

    @Bean("dataSourceBeike")
    public DataSource dataSourceBeike() {
        DatasourceProperties d = new DatasourceProperties();
        BeanUtils.copyProperties(datasourceProperties, d);
        d.setUrl(url);
        return DataSourceHikariBuilder.buildDataSource(d);
    }
}
